package com.qwc.blog.security.entity;

import com.qwc.blog.entity.User;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;

import static com.qwc.blog.security.constant.SecurityConstant.*;

/**
 * @author qwc
 * @Date 2021/10/15 13:59
 */
public class LoginUser implements UserDetails {

    @Getter
    private User user;

    public LoginUser(User user) {
        this.user = user;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (ADMIN_CODE.equals(user.getStatus())) {
            return Collections.singletonList((GrantedAuthority) () -> ADMIN);
        }
        return Collections.singletonList((GrantedAuthority) () -> ORDINARY);
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
